<template>
    <div class="index">
        <Row type="flex" justify="center" align="middle">
            <Col span="24">
            <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
                <div>
                    <FormItem prop="user">
                        <Input type="text" v-model="formInline.username" placeholder="Username">
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                        </Input>
                    </FormItem>
                </div>
                <div>
                    <FormItem prop="password">
                        <Input type="password" v-model="formInline.password" placeholder="Password">
                        <Icon type="ios-locked-outline" slot="prepend"></Icon>
                        </Input>
                    </FormItem>
                </div>
                <div>
                    <FormItem>
                        <Button type="primary" @click="handleSubmit('formInline')">登录</Button>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="handleRest('formInline')">重置</Button>
                    </FormItem>
                </div>
            </Form>
            </Col>
        </Row>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                formInline: {
                    username: '',
                    password: ''
                },
                ruleInline: {
                    username: [
                        {required: true, message: '请填写用户名', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请填写密码', trigger: 'blur'},
                        {type: 'string', min: 1, message: '密码长度不能小于6位', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$store.dispatch('loginByUserInfo', this.formInline).then((req) => {
                            if(req.success){
                                this.$Message.success('登录成功');
                                this.$router.push({ path: '/main' });
                            }else{
                                this.$Message.error(req.msg);
                            }
                        }).catch(err => {
                            console.log('err------------');
                            this.$Message.error(err);
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                });
            },
            handleRest(name){
                this.$refs[name].resetFields();
            }
        }
    };
</script>
<style scoped lang="less">
    .index {
        width: 100%;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        text-align: center;
        h1 {
            height: 150px;
            img {
                height: 100%;
            }
        }
        h2 {
            color: #666;
            margin-bottom: 200px;
            p {
                margin: 0 0 50px;
            }
        }
        .ivu-row-flex {
            height: 100%;
        }
        div {
            margin: 5px 5px 5px 5px;
        }
    }
</style>